package edu.ecu.seng6270.goa.ropa;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;

import javax.swing.*;

import edu.ecu.seng6270.goa.pss.PSSExternal;

import net.miginfocom.swing.MigLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * GUI Panel to present final order price calculation and submission.
 */
public class CheckoutPanel extends JPanel {

	ROPAMain parent;
	Order myOrder;

	private static final long serialVersionUID = -2335098423452402L;
	private JTextField promoEntry;
	private JLabel displayFinalPrice;
	private JLabel displayDiscounts;
	private JLabel displaySubtotal;
	
	private static final Color validColor = new Color(0x008000);
	private static final Color inValidColor = new Color(0x800000);

	CheckoutPanel(final ROPAMain parent) {
		super();
		this.parent = parent;
		this.myOrder = parent.getPendingOrder();

		this.setBackground(new Color(255, 255, 255));
		this.setLayout(new MigLayout("", "[grow]", "[][][fill][][][40px][]"));

		JButton returnToOrderButton = new JButton("Edit My Order");
		returnToOrderButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String backTarget = (myOrder.getIsExpress()) ? "expressOrderPanel" : "customOrderPanel";
				Component[] ccc = parent.cards.getComponents();
				for (Component c : ccc) {
					if (c.getName().equals("checkoutPanel")) {
						parent.cards.remove(c);
						parent.cards.validate();
						parent.moveToCard(backTarget);
					}
				}
			}
		});

		JLabel panelTitle = new JLabel("Review & Submit Your Order");
		panelTitle.setFont(new Font("Tahoma", Font.BOLD, 18));
		this.add(panelTitle, "cell 0 0,alignx center");

		JLabel lblOrderSubtotal = new JLabel("Order Subtotal:");
		add(lblOrderSubtotal, "flowx,cell 0 1,alignx center");

		final JLabel promptForPromo = new JLabel("If you have a promotional code, type it here and press enter:");
		add(promptForPromo, "flowx,cell 0 2,alignx center,aligny top");

		JLabel lblTotalDiscounts = new JLabel("Additional Discounts:");
		add(lblTotalDiscounts, "flowx,cell 0 3,alignx center");

		JLabel lblFinalOrderPrice = new JLabel("Final Order Price:");
		lblFinalOrderPrice.setFont(new Font("Tahoma", Font.BOLD, 14));
		add(lblFinalOrderPrice, "flowx,cell 0 4,alignx center");
		add(returnToOrderButton, "flowx,cell 0 6,alignx center");

		JButton submitOrderButton = new JButton("Submit Order");
		submitOrderButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				PSSExternal.commitOrderToDataStore(myOrder);
				JOptionPane.showMessageDialog(parent.cards, "Your order has been submitted. Thank you!", "Vilkomir Photo Labs", JOptionPane.INFORMATION_MESSAGE);
				parent.cards.removeAll();
				parent.moveToCard("loginPanel");
			}
		});
		add(submitOrderButton, "cell 0 6");

		promoEntry = new JTextField();
		promoEntry.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent arg0) {
				boolean isValid;
				
				isValid = myOrder.validatePromoCode(promoEntry.getText());
				if (isValid) {
					promoEntry.setForeground(validColor);
					promptForPromo.setForeground(validColor);
					promptForPromo.setText("Promotional Code Validated");
				} else {
					promoEntry.setForeground(inValidColor);
					promptForPromo.setText("Promotional Code is Not Valid");
					promptForPromo.setForeground(inValidColor);
				}
				refreshPriceDisplay();
			}
		});
		add(promoEntry, "cell 0 2,alignx center");
		promoEntry.setColumns(10);

		displayFinalPrice = new JLabel("$00.00");
		displayFinalPrice.setForeground(new Color(0, 102, 0));
		displayFinalPrice.setFont(new Font("Tahoma", Font.BOLD, 14));
		add(displayFinalPrice, "cell 0 4");

		displayDiscounts = new JLabel("( $00.00 )");
		add(displayDiscounts, "cell 0 3");

		displaySubtotal = new JLabel("$00.00");
		add(displaySubtotal, "cell 0 1");
		
		// If not eligible, don't ask
		if (! myOrder.getPromoEligible() || !myOrder.getIsExpress()) {
			this.remove(promoEntry);
			this.validate();
			promptForPromo.setText("Order is not eligible for promotions.");
		}
		
		myOrder.validatePromoCode("");
		refreshPriceDisplay();
	}
	
	void refreshPriceDisplay() {

		myOrder.calculateOrderPrice();

		displaySubtotal.setText(String.format("$%.2f", myOrder.getNonDiscountedPrice()));
		displayDiscounts.setText(String.format("( $%.2f )", myOrder.getVolumeDiscount()));
		displayFinalPrice.setText(String.format("$%.2f", myOrder.getDiscountedPrice()));
	}
	
	
	
}
